/*--------------------------------------------------------------------------
 *  Copyright 2011 Taro L. Saito
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// XerialJ
//
// SimpleActorMediator.java
// Since: 2011/06/13 18:26:51
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.silk.weaver.cloud;

import java.util.HashMap;

import org.xerial.core.XerialException;
import org.xerial.util.TypeInfo;
import org.xerial.util.log.Logger;

public class SimpleActorMediator implements ActorMediator
{
    private static Logger                               _logger       = Logger.getLogger(SimpleActorMediator.class);

    private HashMap<String, Class< ? extends Function>> functionTable = new HashMap<String, Class< ? extends Function>>();
    private HashMap<String, String>                     connection    = new HashMap<String, String>();

    @Override
    public void register(String name, Class< ? extends Function> function) {
        this.functionTable.put(name, function);
    }

    @Override
    public void connect(String sender, String reciever) {
        this.connection.put(sender, reciever);
    }

    @Override
    public void sendRequest(String actorName, Object message) {
        Class< ? extends Function> fType = this.functionTable.get(actorName);
        if (fType == null)
            throw new IllegalArgumentException("Unknown actor: " + actorName);
        try {

            // TODO
            // SilkLens.loadSilk(resultType, silkReader)

            Function f = TypeInfo.createInstance(fType);

            f.execute(new Reporter() {
                @Override
                public void emit(Object result) {
                    _logger.info("Recieved a result: " + result);
                }
            });
        }
        catch (XerialException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
